
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:laos_mall/common/style/extension_util.dart';
import 'package:laos_mall/common/widght/textfiled_widget.dart';

class NumberMallWidget extends StatefulWidget {
   NumberMallWidget({super.key,this.onChanged,required this.number,this.isEnable = true});
  final ValueChanged<int>? onChanged;
  int number;
   final bool isEnable;
  @override
  State<NumberMallWidget> createState() => _NumberMallWidgetState();
}

class _NumberMallWidgetState extends State<NumberMallWidget> {
  int number = 0;
  TextEditingController _textEditingController = TextEditingController();
  @override
  void initState() {
    number = widget.number;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var numStr = widget.number.toString();
    _textEditingController.text = numStr;
    return Row(children: [
      // IconButton(onPressed: (){
      //   if(widget.number <= 1) return;
      //   clickBtn(false);
      // },icon: Icon(Icons.remove,size: 17,color: widget.number <= 1 ? Color(0xFF999999) : Colors.black)),
     Icon(Icons.remove,size: 17,color: widget.number <= 1 ? Color(0xFF999999) : Colors.black).addContainer(width: 40.w,height: 40.w).addGestureTap(() {
       if(widget.number <= 1) return;
         clickBtn(false);
     }),
      TextFormFieldWidget(type: TextFormFieldWidgetType.choose,controller: _textEditingController,subTitle: numStr,keyboardType: TextInputType.numberWithOptions(),showArrowIcon: false,titleWidth: 0,hintText: "",subTitleTextAlign: TextAlign.center,padding: EdgeInsets.zero,spacing: 0,onChanged: (value){
        var num = int.tryParse(value);
        if(widget.onChanged != null) {
          widget.onChanged!(num ?? 0);
        }
      },contentPadding: EdgeInsets.zero,).addContainer(height: 30.w,width: 30.w),
      Icon(Icons.add,size: 17,color: widget.isEnable ? Colors.black : Color(0xFF999999),).addContainer(width: 40.w,height: 40.w).addGestureTap(() {
        if(widget.isEnable) {
          clickBtn(true);
        }
      }),
    ],).addContainer(height: 32.w);
  }

  clickBtn(bool isAdd){
    if(isAdd){
      widget.number += 1;
    }else{
      widget.number -= 1;
    }
    if(widget.onChanged != null) {
      widget.onChanged!(widget.number);
    }
  }
}
